//
//  UIView+InputAccessory.m
//  AddressBookCloud
//
//  Created by 林宁宁 on 2020/4/14.
//  Copyright © 2020 林宁宁. All rights reserved.
//

#import "UIView+YHInputAccessory.h"
#import "UIButton+YH.h"
#import "UIColor+YHStyle.h"
#import "UIFont+YH.h"
#import "YHDefines.h"

@implementation UIView (YHInputAccessory)


-(void)showAccessoryView:(void (^_Nullable)(UIView *))customView
                 leftBtn:(void (^_Nullable)(UIButton *))btnLeftBlock
                rightBtn:(void (^_Nullable)(UIButton *))btnRightBlock
               leftBlock:(void (^_Nullable)(void))leftBlock
              rightBlock:(void (^_Nullable)(void))rightBlock{
    
    
    if(![self isKindOfClass:[UITextView class]] &&
       ![self isKindOfClass:[UITextField class]] &&
       ![self isKindOfClass:[UISearchBar class]]){
        return;
    }
    
    UIView * toolV = [[UIView alloc] initWithFrame:CGRectMake(0, 0, [UIScreen mainScreen].bounds.size.width, 44)];
    toolV.backgroundColor = [UIColor yh_separator];
    if(customView){
        customView(toolV);
    }
    
    if(btnLeftBlock){
        UIButton * btnLeft = [UIButton buttonWithType:UIButtonTypeCustom];
        btnLeft.titleLabel.font = [UIFont yh_pfOfSize:14];
        btnLeft.frame = CGRectMake(0, 0, 60, CGRectGetHeight(toolV.frame));
        [btnLeft setTitleColor:[UIColor yh_h1] forState:UIControlStateNormal];
        [btnLeft addActionHandler:^(UIButton * sender, NSInteger tag) {
            if(leftBlock){
                leftBlock();
            }
        }];
        btnLeftBlock(btnLeft);
        [toolV addSubview:btnLeft];
    }
    
    if(btnRightBlock){
        UIButton * btnRight = [UIButton buttonWithType:UIButtonTypeCustom];
        btnRight.titleLabel.font = [UIFont yh_pfOfSize:14];
        btnRight.frame = CGRectMake(CGRectGetWidth(toolV.frame)-60, 0, 60, CGRectGetHeight(toolV.frame));
        [btnRight setTitleColor:[UIColor yh_h1] forState:UIControlStateNormal];
        [btnRight addActionHandler:^(UIButton * sender, NSInteger tag) {
            if(rightBlock){
                rightBlock();
            }
        }];
        btnRightBlock(btnRight);
        [toolV addSubview:btnRight];
    }
    
    if([self isKindOfClass:[UITextView class]]){
        UITextView * textV = (UITextView *)self;
        textV.inputAccessoryView = toolV;
    }
    else if([self isKindOfClass:[UITextField class]]){
        UITextField * textF = (UITextField *)self;
        textF.inputAccessoryView = toolV;
    }
    else if([self isKindOfClass:[UISearchBar class]]){
        UISearchBar * textF = (UISearchBar *)self;
        textF.inputAccessoryView = toolV;
    }
}

/** 完成的输入按钮*/
- (void)showAccessoryDown:(void(^_Nullable)(void))finishBlock{
    
    WS(weakSelf);
    [self showAccessoryView:nil leftBtn:nil rightBtn:^(UIButton *btn) {
        
        [btn setTitle:@"完成" forState:UIControlStateNormal];
        
    } leftBlock:nil rightBlock:^{
        
        if([weakSelf isKindOfClass:[UITextView class]]){
            UITextView * textV = (UITextView *)weakSelf;
            [textV resignFirstResponder];
        }else if([weakSelf isKindOfClass:[UITextField class]]){
            UITextField * textF = (UITextField *)weakSelf;
            [textF resignFirstResponder];
        }else if([weakSelf isKindOfClass:[UISearchBar class]]){
            UISearchBar * textF = (UISearchBar *)weakSelf;
            [textF resignFirstResponder];
        }
        if(finishBlock){
            finishBlock();
        }
        
    }];
    
}

/** 关闭的输入按钮*/
- (void)showAccessoryClose:(void(^_Nullable)(void))closeBlock{
    
    WS(weakSelf);
    [self showAccessoryView:nil leftBtn:nil rightBtn:^(UIButton *btn) {
        
        [btn setTitle:@"关闭" forState:UIControlStateNormal];
        
    } leftBlock:nil rightBlock:^{
        
        if([weakSelf isKindOfClass:[UITextView class]]){
            UITextView * textV = (UITextView *)weakSelf;
            [textV resignFirstResponder];
        }else if([weakSelf isKindOfClass:[UITextField class]]){
            UITextField * textF = (UITextField *)weakSelf;
            [textF resignFirstResponder];
        }
        if(closeBlock){
            closeBlock();
        }
    }];
    
}


@end
